package com.syblood.app.camera.watermark.render;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.opengl.GLES30;
import android.util.Log;

import com.syblood.app.utils.LogUtil;
import com.syblood.app.camera.watermark.utils.GlesUtil;

import java.util.Timer;
import java.util.TimerTask;

/**
 * @CreateTime : 2022/11/15
 * @Author : JiChangkong
 * @Description :
 */
public class WaterMarkRenderDrawer extends BaseRenderDrawer {
    private final static String TAG = "wanwan_" + WaterMarkRenderDrawer.class.getSimpleName();
    private int mMarkTextureId;
    private int mMarkTextureId2;
    private int mInputTextureId;
    private Bitmap mBitmap;
    private Bitmap mBitmap2;
    private int avPosition;
    private int afPosition;
    private int sTexture;

    private int x = 0;
    private int y = 0;

    Timer timer;
    TimerTask task;

    public TimerTask createrTask() {
        return new TimerTask() {
            public void run() {
                if(mBitmap!=null){
                    return;
                }
//                if (width > 0) {
//                    Bitmap waterMarkBitmap = WaterMarkManager.getInstance().getFullWaterMarkBitmap(0,0);
//
//                    Log.d(TAG, "wanwan  waterMarkBitmap.width = " + waterMarkBitmap.getWidth() + " , waterMarkBitmap.height = " + waterMarkBitmap.getHeight());
//                    Log.d(TAG, "wanwan width = " + width + " , height = " + height);
//
//                    float scale = 1.0F * width / (waterMarkBitmap.getWidth());
//                    if (scale < 1.0F * height / (waterMarkBitmap.getHeight())) {
//                        scale = 1.0F * height / (waterMarkBitmap.getHeight());
//                    }
//                    Matrix matrix = new Matrix();
//                    matrix.postScale(scale, scale);
//                    mBitmap = Bitmap.createBitmap(waterMarkBitmap, 0, 0, waterMarkBitmap.getWidth(), waterMarkBitmap.getHeight(), matrix, false);
//                    Log.d(TAG, "wanwan  mBitmap.width = " + mBitmap.getWidth() + " , mBitmap.height = " + mBitmap.getHeight());
//                    waterMarkBitmap.recycle();
//                }
            }
        };
    }

    public WaterMarkRenderDrawer(Context context) {

//        x = WaterMarkManager.getInstance().getX();
//        y = WaterMarkManager.getInstance().getY();

        timer = new Timer();
        timer.schedule(createrTask(), 1000, 1000); //启动timer
    }

    @Override
    public void destroy() {
        super.destroy();
        timer.cancel();
        timer = null;
        if(mBitmap!=null){
            mBitmap.recycle();
            mBitmap = null;
        }
    }

    @Override
    public void setInputTextureId(int textureId) {
        this.mInputTextureId = textureId;
    }

    @Override
    public int getOutputTextureId() {
        return mInputTextureId;
    }

    @Override
    protected void onCreated() {

    }

    @Override
    public void startRecorde() {
        LogUtil.d("startRecorde");
    }

    @Override
    public void stopRecording() {
        LogUtil.d("stopRecording");
    }

    @Override
    protected void onChanged(int width, int height) {
        if (mBitmap != null) {
            mMarkTextureId = GlesUtil.loadBitmapTexture(mBitmap);
        }

        avPosition = GLES30.glGetAttribLocation(mProgram, "av_Position");
        afPosition = GLES30.glGetAttribLocation(mProgram, "af_Position");
        sTexture = GLES30.glGetUniformLocation(mProgram, "sTexture");
    }

    @Override
    public void draw(long timestamp, float[] transformMatrix) {
//        Log.d("draw_check", "watermark draw");
        useProgram();

        if (mBitmap != null) {
            int beginX = 0;
            int beginY = 0;
            viewPort(beginX, beginY, mBitmap.getWidth(), mBitmap.getHeight());
            mMarkTextureId = GlesUtil.loadBitmapTexture(mBitmap);
        }


        GLES30.glDisable(GLES30.GL_DEPTH_TEST);
        GLES30.glEnable(GLES30.GL_BLEND);
        GLES30.glBlendFunc(GLES30.GL_SRC_COLOR, GLES30.GL_DST_ALPHA);
        onDraw();
        GLES30.glDisable(GLES30.GL_BLEND);
    }

    @Override
    protected void onDraw() {
        GLES30.glEnableVertexAttribArray(avPosition);
        GLES30.glEnableVertexAttribArray(afPosition);
        //设置顶点位置值
        //GLES30.glVertexAttribPointer(avPosition, CoordsPerVertexCount, GLES30.GL_FLOAT, false, VertexStride, mVertexBuffer);
        //设置纹理位置值
        //GLES30.glVertexAttribPointer(afPosition, CoordsPerTextureCount, GLES30.GL_FLOAT, false, TextureStride, mFrameTextureBuffer);
        GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mVertexBufferId);
        GLES30.glVertexAttribPointer(avPosition, CoordsPerVertexCount, GLES30.GL_FLOAT, false, 0, 0);
        GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mFrameTextureBufferId);
        GLES30.glVertexAttribPointer(afPosition, CoordsPerTextureCount, GLES30.GL_FLOAT, false, 0, 0);
        GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
        GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
        if (mMarkTextureId2 > 0) {
            GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mMarkTextureId2);
        }

        if (mMarkTextureId > 0) {
            GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mMarkTextureId);
        }

        GLES30.glUniform1i(sTexture, 0);
        //绘制 GLES30.GL_TRIANGLE_STRIP:复用坐标
        GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, VertexCount);
        GLES30.glDisableVertexAttribArray(avPosition);
        GLES30.glDisableVertexAttribArray(afPosition);
    }

    @Override
    protected String getVertexSource() {
        final String source =
                "attribute vec4 av_Position; " +
                        "attribute vec2 af_Position; " +
                        "varying vec2 v_texPo; " +
                        "void main() { " +
                        "    v_texPo = af_Position; " +
                        "    gl_Position = av_Position; " +
                        "}";
        return source;
    }

    @Override
    protected String getFragmentSource() {
        final String source =
                "precision mediump float; " +
                        "varying vec2 v_texPo; " +
                        "uniform sampler2D sTexture; " +
                        "void main() { " +
                        "   gl_FragColor = texture2D(sTexture, v_texPo); " +
                        "} ";
        return source;
    }
}
